************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 2 (Balance Checks Corresponding to Main Regression Specifications)
* Last edited: 07 May 2021
************************************************
	
	clear all
	set more off
	set matsize 800
		
	use "$d/main_index_dataset.dta", clear
	
	keep pid wellbeing_index_base cogindex_pre pref_pre
	collapse wellbeing_index_base cogindex_pre pref_pre, by (pid)
	
	tempfile index
	save "`index'"
	 
use "$d/balance_dataset.dta", clear	

	merge m:1 pid using "`index'"
	keep if _merge==3
	
	rename wb_bsl wb_bsl_old
	rename wellbeing_index_base wb_bsl 
	rename cogindex_pre cg_bsl
	rename pref_pre pref_bsl
	
	
	cap egen tag = tag(pid) if post_treatment == 1
	
local varlist "female age no_of_children education computer_comfort unemployed bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned health_bsl wb_bsl baseline_pvt baseline_hf baseline_corsi baseline_typing baseline_office baseline_prod baseline_earnings bsl_attendance_2 baseline_savings prior_savings_1000 cg_bsl pref_bsl"
foreach var of local varlist {

************ SLEEP TREATMENTS **************************************************

* Control mean

	sum `var' if treatment_group==0 & tag==1 
	local mean_`var'_cont_t = string(r(mean), "%3.2f")
	local se_`var'_cont_t = string(r(sd)/sqrt(r(N)), "%3.2f")

* Treatment mean 

	replace treatment_group=1 if treatment_group==2 
	sum `var' if treatment_group==1 & tag==1
	local mean_`var'_treat_t = string(r(mean), "%3.2f")
	local se_`var'_treat_t = string(r(sd)/sqrt(r(N)), "%3.2f")
	
* P-values of differences

	reg `var' i.treatment_group if tag==1 //P-values for coefficients are p-values for mean-differences!

	mat def B = e(b)
	mat def V = e(V)
	local pval_`var'_0_1_t = string((2 * ttail(e(df_r), abs(B[1,2]/sqrt(V[2,2])))), "%9.2f") // Difference between Control and Treatment

	reg `var' treat_pool  if tag==1 // Standard error is p-value for difference between control and pooled!
	mat def B = e(b)
	mat def V = e(V)

	local pval_`var'_0_12_t = string((2 * ttail(e(df_r), abs(B[1,1]/sqrt(V[1,1])))), "%9.2f") // Difference between Control and Pooled Treat

************ NAP TREATMENTS **************************************************

* Control mean

	sum `var' if treat_nap==0 & tag==1 
	local mean_`var'_cont_n = string(r(mean), "%3.2f")
	local se_`var'_cont_n = string(r(sd)/sqrt(r(N)), "%3.2f")

* Nap mean

	sum `var' if treatment_group==1 & tag==1
	local mean_`var'_treat_n = string(r(mean), "%3.2f")
	local se_`var'_treat_n = string(r(sd)/sqrt(r(N)), "%3.2f")

* P-values of differences

	reg `var' treat_nap if tag==1 //P-values for coefficients are p-values for mean-differences!
	mat def B = e(b)
	mat def V = e(V)
	local pval_`var'_0_1_n = string((2 * ttail(e(df_r), abs(B[1,1]/sqrt(V[1,1])))), "%9.2f") // Difference between Control and Nap
}

* Counting obs

	count if treatment_group==0 & tag==1
	local N_control = string(r(N), "%3.0f")
	count if treatment_group==1 & tag==1
	local N_treat = string(r(N), "%3.0f")
	count if treat_nap==0 & tag==1
	local N_controlnap = string(r(N), "%3.0f")
	count if treat_nap==1 & tag==1
	local N_treatnap = string(r(N), "%3.0f")

* Joint orthogonality by sub group

	* Demographics
	local varlist "female age no_of_children education"
	
		foreach treatment in treat_pool treat_s treat_s_i treat_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_joint_`treatment'_demo = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Baseline Sleep
	local varlist "bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned"
	
		foreach treatment in treat_pool treat_s treat_s_i treat_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_joint_`treatment'_sleep = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Well Being, Cognition and Preferences 
	local varlist "health_bsl wb_bsl baseline_pvt baseline_hf baseline_corsi cg_bsl pref_bsl"
	
		foreach treatment in treat_pool treat_s treat_s_i treat_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_joint_`treatment'_hwbc = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Baseline Work and Savings
	local varlist "baseline_typing baseline_office baseline_prod baseline_earnings  bsl_attendance_2 baseline_savings prior_savings_1000"
	*local varlist "bsl_attendance_2"
		foreach treatment in treat_pool treat_s treat_s_i treat_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_joint_`treatment'_work = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
		
	local varlist "female age no_of_children education bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned health_bsl wb_bsl baseline_pvt baseline_hf baseline_corsi baseline_typing baseline_office baseline_prod baseline_earnings  bsl_attendance_2 baseline_savings prior_savings_1000"
	
		foreach treatment in treat_pool treat_s treat_s_i treat_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_joint_`treatment'_all = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}


*Exporting

file open f using "$oa/Tables/TableA2a_balance1_pooled_index.tex", write replace
				file write f "\begin{tabular}{l*{7}{c}}" _n ///+
				"\toprule" _n ///
				" &\multicolumn{3}{c}{\textbf{Night-Sleep Treatments}} & \multicolumn{3}{c}{\textbf{Nap Treatments}} \\" _n ///
				"\cmidrule(lr){2-4}\cmidrule(lr){5-7} &\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Treatment}&\multicolumn{1}{c}{1=2}&\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Treatment}&\multicolumn{1}{c}{4=5} \\" _n ///
				" & (1) & (2) & (3) & (4) & (5) & (6) \\" _n ///
				"\midrule" _n ///
				"\textit{Panel A. Demographics} & & & & & & & \\" _n ///
				"Female & `mean_female_cont_t' & `mean_female_treat_t' & `pval_female_0_12_t' & `mean_female_cont_n' & `mean_female_treat_n' & `pval_female_0_1_n' \\" _n ///
				" & (`se_female_cont_t') & (`se_female_treat_t') & & (`se_female_cont_n') & (`se_female_treat_n') &  \\" _n ///
				"Age & `mean_age_cont_t' & `mean_age_treat_t' & `pval_age_0_12_t' & `mean_age_cont_n' & `mean_age_treat_n' & `pval_age_0_1_n' \\" _n ///
				" & (`se_age_cont_t') & (`se_age_treat_t') &  & (`se_age_cont_n') & (`se_age_treat_n') &  \\" _n ///
				"Number of Children & `mean_no_of_children_cont_t' & `mean_no_of_children_treat_t' & `pval_no_of_children_0_12_t' & `mean_no_of_children_cont_n' & `mean_no_of_children_treat_n' & `pval_no_of_children_0_1_n' \\" _n ///
				" & (`se_no_of_children_cont_t') & (`se_no_of_children_treat_t') &  & (`se_no_of_children_cont_n') & (`se_no_of_children_treat_n') &  \\" _n ///
				"Years of Education & `mean_education_cont_t' & `mean_education_treat_t' & `pval_education_0_12_t' & `mean_education_cont_n' & `mean_education_treat_n' & `pval_education_0_1_n' \\" _n ///
				" & (`se_education_cont_t') & (`se_education_treat_t') &  & (`se_education_cont_n') & (`se_education_treat_n') &  \\" _n ///
				"Familiar with Computer & `mean_computer_comfort_cont_t' & `mean_computer_comfort_treat_t' & `pval_computer_comfort_0_12_t' & `mean_computer_comfort_cont_n' & `mean_computer_comfort_treat_n' & `pval_computer_comfort_0_1_n' \\" _n ///
				" & (`se_computer_comfort_cont_t') & (`se_computer_comfort_treat_t') &  & (`se_computer_comfort_cont_n') & (`se_computer_comfort_treat_n') &  \\" _n ///
				"Unemployed & `mean_unemployed_cont_t' & `mean_unemployed_treat_t' & `pval_unemployed_0_12_t' & `mean_unemployed_cont_n' & `mean_unemployed_treat_n' & `pval_unemployed_0_1_n' \\" _n ///
				" & (`se_unemployed_cont_t') & (`se_unemployed_treat_t') &  & (`se_unemployed_cont_n') & (`se_unemployed_treat_n') &  \\ \\" _n ///
				"\textit{Panel B. Baseline Sleep} & & & & & & & \\" _n ///
				"Self-Reported Night Sleep (Hrs) & `mean_bsl_self_report_cont_t' & `mean_bsl_self_report_treat_t' & `pval_bsl_self_report_0_12_t' & `mean_bsl_self_report_cont_n' & `mean_bsl_self_report_treat_n' & `pval_bsl_self_report_0_1_n' \\" _n ///
				" & (`se_bsl_self_report_cont_t') & (`se_bsl_self_report_treat_t') &  & (`se_bsl_self_report_cont_n') & (`se_bsl_self_report_treat_n') &  \\" _n ///
				"Actigraph Night Sleep (Hrs) & `mean_bsl_avg_sleep_cont_t' & `mean_bsl_avg_sleep_treat_t' & `pval_bsl_avg_sleep_0_12_t' & `mean_bsl_avg_sleep_cont_n' & `mean_bsl_avg_sleep_treat_n' & `pval_bsl_avg_sleep_0_1_n' \\" _n ///
				" & (`se_bsl_avg_sleep_cont_t') & (`se_bsl_avg_sleep_treat_t') &  & (`se_bsl_avg_sleep_cont_n') & (`se_bsl_avg_sleep_treat_n') &  \\" _n ///
				"Actigraph Time in Bed (Hrs) & `mean_baseline_bed_cont_t' & `mean_baseline_bed_treat_t' & `pval_baseline_bed_0_12_t' & `mean_baseline_bed_cont_n' & `mean_baseline_bed_treat_n' & `pval_baseline_bed_0_1_n' \\" _n ///
				" & (`se_baseline_bed_cont_t') & (`se_baseline_bed_treat_t') &  & (`se_baseline_bed_cont_n') & (`se_baseline_bed_treat_n') &  \\" _n ///
				"Sleep Efficiency & `mean_baseline_sleep_eff_cont_t' & `mean_baseline_sleep_eff_treat_t' & `pval_baseline_sleep_eff_0_12_t' & `mean_baseline_sleep_eff_cont_n' & `mean_baseline_sleep_eff_treat_n' & `pval_baseline_sleep_eff_0_1_n' \\" _n ///
				" & (`se_baseline_sleep_eff_cont_t') & (`se_baseline_sleep_eff_treat_t') &  & (`se_baseline_sleep_eff_cont_n') & (`se_baseline_sleep_eff_treat_n') &  \\" _n ///
				"Number of Sleep Devices Owned & `mean_no_device_owned_cont_t' & `mean_no_device_owned_treat_t' & `pval_no_device_owned_0_12_t' & `mean_no_device_owned_cont_n' & `mean_no_device_owned_treat_n' & `pval_no_device_owned_0_1_n' \\" _n ///
				" & (`se_no_device_owned_cont_t') & (`se_no_device_owned_treat_t') &  & (`se_no_device_owned_cont_n') & (`se_no_device_owned_treat_n') &  \\ \\" _n ///
				"\midrule" _n ///
				"Number of Participants & `N_control' & `N_treat' & & `N_controlnap' & `N_treatnap' \\" _n ///
				"\bottomrule \\" _n ///
				"\end{tabular}" _n 
		file close f
	
		
		file open f using "$oa/Tables/TableA2b_balance2_pooled_index.tex", write replace
				file write f "\begin{tabular}{l*{7}{c}}" _n ///+
				"\toprule" _n ///
				" &\multicolumn{3}{c}{\textbf{Night-Sleep Treatments}} & \multicolumn{3}{c}{\textbf{Nap Treatments}} \\" _n ///
				"\cmidrule(lr){2-4}\cmidrule(lr){5-7} &\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Treatment}&\multicolumn{1}{c}{1=2}&\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Treatment}&\multicolumn{1}{c}{4=5} \\" _n ///
				" & (1) & (2) & (3) & (4) & (5) & (6) \\" _n ///
				"\midrule" _n ///
				"\textit{Panel C. Wellbeing, Cognition, and Preferences} & & & & & & & \\" _n ///
				"Baseline Wellbeing & `mean_wb_bsl_cont_t' & `mean_wb_bsl_treat_t' & `pval_wb_bsl_0_12_t' & `mean_wb_bsl_cont_n' & `mean_wb_bsl_treat_n' & `pval_wb_bsl_0_1_n' \\" _n ///
				" & (`se_wb_bsl_cont_t') & (`se_wb_bsl_treat_t') &  & (`se_wb_bsl_cont_n') & (`se_wb_bsl_treat_n') &  \\" _n ///
				"Baseline Cognition & `mean_cg_bsl_cont_t' & `mean_cg_bsl_treat_t' & `pval_cg_bsl_0_12_t' & `mean_cg_bsl_cont_n' & `mean_cg_bsl_treat_n' & `pval_cg_bsl_0_1_n' \\" _n ///
				" & (`se_cg_bsl_cont_t') & (`se_cg_bsl_treat_t') &  & (`se_cg_bsl_cont_n') & (`se_cg_bsl_treat_n') &  \\" _n ///
				"Baseline Preferences & `mean_pref_bsl_cont_t' & `mean_pref_bsl_treat_t' & `pval_pref_bsl_0_12_t' & `mean_pref_bsl_cont_n' & `mean_pref_bsl_treat_n' & `pval_pref_bsl_0_1_n' \\" _n ///
				" \\ " _n ///
				" & (`se_pref_bsl_cont_t') & (`se_pref_bsl_treat_t') &  & (`se_pref_bsl_cont_n') & (`se_pref_bsl_treat_n') &  \\" _n ///
				"\textit{Panel D. Baseline Work} & & & & & & & \\" _n ///
				"Typing Time (Hrs) & `mean_baseline_typing_cont_t' & `mean_baseline_typing_treat_t' & `pval_baseline_typing_0_12_t' & `mean_baseline_typing_cont_n' & `mean_baseline_typing_treat_n' & `pval_baseline_typing_0_1_n' \\" _n ///
				" & (`se_baseline_typing_cont_t') & (`se_baseline_typing_treat_t') &  & (`se_baseline_typing_cont_n') & (`se_baseline_typing_treat_n') &  \\" _n ///
				"Time in Office (Hrs) & `mean_baseline_office_cont_t' & `mean_baseline_office_treat_t' & `pval_baseline_office_0_12_t' & `mean_baseline_office_cont_n' & `mean_baseline_office_treat_n' & `pval_baseline_office_0_1_n' \\" _n ///
				" & (`se_baseline_office_cont_t') & (`se_baseline_office_treat_t') &  & (`se_baseline_office_cont_n') & (`se_baseline_office_treat_n') &  \\" _n ///
				"Productivity & `mean_baseline_prod_cont_t' & `mean_baseline_prod_treat_t' & `pval_baseline_prod_0_12_t' & `mean_baseline_prod_cont_n' & `mean_baseline_prod_treat_n' & `pval_baseline_prod_0_1_n' \\" _n ///
				" & (`se_baseline_prod_cont_t') & (`se_baseline_prod_treat_t') &  & (`se_baseline_prod_cont_n') & (`se_baseline_prod_treat_n') &  \\" _n ///
				"Earnings & `mean_baseline_earnings_cont_t' & `mean_baseline_earnings_treat_t' & `pval_baseline_earnings_0_12_t' & `mean_baseline_earnings_cont_n' & `mean_baseline_earnings_treat_n' & `pval_baseline_earnings_0_1_n' \\" _n ///
				" & (`se_baseline_earnings_cont_t') & (`se_baseline_earnings_treat_t') &  & (`se_baseline_earnings_cont_n') & (`se_baseline_earnings_treat_n') &  \\" _n ///
				"Attendance & `mean_bsl_attendance_2_cont_t' & `mean_bsl_attendance_2_treat_t' & `pval_bsl_attendance_2_0_12_t' & `mean_bsl_attendance_2_cont_n' & `mean_bsl_attendance_2_treat_n' & `pval_bsl_attendance_2_0_1_n' \\" _n ///
				" & (`se_bsl_attendance_2_cont_t') & (`se_bsl_attendance_2_treat_t')  &  & (`se_bsl_attendance_2_cont_n') & (`se_bsl_attendance_2_treat_n') &  \\" _n ///
				"\midrule" _n ///
				"Joint Orthogonality Test & & & `p_val_joint_treat_pool_all' & & & `p_val_joint_treat_nap_all' \\" _n ///
				"Number of Participants & `N_control' & `N_treat' & & `N_controlnap' & `N_treatnap' &\\" _n ///
				"\bottomrule \\" _n ///
				"\end{tabular}" _n 
		file close f

